package com.itheima.controller;


import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/depts")
    public Result selectDepts(){
        List<Dept> deptList = deptService.selectDepts();
        return Result.success(deptList);
    }

    @DeleteMapping("/depts/{id}")
    public Result deleteDept(@PathVariable Integer id){
        deptService.deleteDept(id);
        return Result.success();
    }

    @PostMapping("/depts")
    public Result insertDept(@RequestBody Dept dept){
        deptService.insertDept(dept);
        return Result.success();
    }

    @GetMapping("/depts/{id}")
    public Result selectDeptById(@PathVariable Integer id){
        Dept dept = deptService.selectDeptById(id);
        return Result.success(dept);
    }

    @PutMapping("/depts")
    public Result updateDeptName(@RequestBody Dept dept){
        deptService.updateDeptName(dept);
        return Result.success();
    }
}
